<?php
class ValidformComponent extends Object{
        var $regex=null;
        var $check=null;
        var $error=array();
        var $msg=null;
        var $errmsg=array();
        var $combinedMsg=array();
        var $combinedError=array();

        function _check($errmsg) {
            //debug(preg_match($this->regex, $this->check));
		if (preg_match($this->regex, $this->check)) {
			$this->error[] = false;
		} else {
			$this->error[] = true;
                        $this->errmsg[]= $errmsg;
		}
              //  debug($this->error);
	}
        function validateArray($myvalidation){
            $this->combinedMsg=array();
            if (is_array($myvalidation)){
                foreach($myvalidation as $m){
                    //debug($this->validate($m['data'], $m['rules'], $m['fieldName']));
                    if(!$this->validate($m['data'], $m['rules'], $m['fieldName'])){
                        
                        $this->combinedMsg[]=$this->msg;
                        $this->combinedError[]=true;
                    }
                }

                $this->msg=implode('<br/>',  $this->combinedMsg);
                $this->msg.='<br/>';
                //debug($this->msg);
                if (in_array(true, $this->combinedError)){
                    return false;
                }else{
                    return true;
                }
            }
        }
        function validate($data,$rules,$fielName){
            $this->msg=null; $this->errmsg=array();
                $this->error=array();
            $this->msg=$fielName." : ";

            foreach($rules as $rule){
                $this->$rule($data);
            }
            //debug($this->error);
            $this->msg.= implode(", ",  $this->errmsg);
            if (in_array(true, $this->error)){
                return false;
            }else{
            return true;}
        }
        function alphaNumeric($check) {
		$this->check = $check;
		if (empty($this->check) && $this->check != '0') {
			return false;
		}
		$this->regex = '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}\-\s]+$/mu';
                $this->_check('not alphanumeric');
	}
        function blank($check) {
            if (strlen($check)>0){
		$this->check = $check;
		$this->regex = '/[^\s]/';
		$this->_check('blank');
            }else{
                $this->error[] = true;
                $this->errmsg[]= 'blank';
            }

	}
        function email($check){
            $this->check = $check;
            $this->regex ='/^([a-zA-Z0-9]+(([\.\-\_]?[a-zA-Z0-9]+)+)?)\@(([a-zA-Z0-9]+[\.\-\_])+[a-zA-Z]{2,4})$/';
            $this->_check('is nor a valid email');
        }
        function getMsg(){
            return $this->msg;
        }
	function notEmpty($check) {
		
		$this->check = $check;
		if (empty($this->check) && $this->check != '0') {
			$this->error[] = true;
                        $this->errmsg[]= 'required';
		}  else {
                   $this->regex = '/[^\s]+/m';
		 $this->_check('required'); 
                }
		
	}
        function date($check, $format = 'yy-mm-dd') {
		$this->check = $check;
                
		$regex['dmy'] = '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%';
		$regex['mdy'] = '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%';
		$regex['ymd'] = '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]))))$%';
		$regex['dMy'] = '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]\\d)\\d{2})$/';
		$regex['Mdy'] = '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/';
		$regex['My'] = '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]|[2-9]\\d)\\d{2})$%';
		$regex['my'] = '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))$%';
                $regex['yy-mm-dd'] ='%^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$%';

                $this->regex = $regex[$format];
                $this->_check('invalid date');
	}
        function numeric($check) {
		if(!is_numeric($check)){
                    $this->error[] = true;
                $this->errmsg[]= 'not numeric';
                }
	}
}
?>
